<script>
	// #ifdef APP-PLUS
		var Color = plus.android.importClass("android.graphics.Color");
		plus.android.importClass("android.view.Window");
		var mainActivity = plus.android.runtimeMainActivity();
		var window_android = mainActivity.getWindow();
		window_android.setNavigationBarColor(Color.parseColor("#f3f3f3"));
	// #endif
	import {
		setRequestValue
	} from '@/common/request.js'
	import storeInits from '@/store/storeInits.js'
	import routingIntercept from '@/utils/permission.js'
	import {sub} from "./common/event.js"
	import commonData from "./common/common.js"
	export default {
		onLaunch: function() {
			const _this = this
			console.log('App Launch')
			// 初始化
			// 拦截路由
			// routingIntercept()
			// 将统一的请求导出
			setRequestValue({
				get: this.$u.get,
				post: this.$u.post,
				put: this.$u.put,
				'delete': this.$u.delete,
			})
			// store初始化
			storeInits.initAllStore({})
			// 注册全局事件
			sub(`socket_passapply`,function(){
				uni.showToast({
					title:"好友通过了你的申请",
					icon:"none"
				})
				_this.$store.dispatch({
					type:"address/refreshFriends",
					payload:{}
				})
				_this.$store.dispatch(`address/initFriendsAndApplySum`,{})
				_this.playSound()
			})
			
			sub(`socket_videocallinvite`,function(e){
				console.log(_this.$store.state.common.status)
				_this.playSoundl()
			})
			
			sub(`socket_fmessage_error`,function(e){
				console.log(114,e.message)
				uni.showToast({
					title:e.message,
					icon:"none"
				})
			})
			
			sub(`socket_fmessage`,function(e){
				console.log(_this.$store.state.chats.current)
				if(e.user_id == _this.$store.state.user.user.id || ( `f_${e.user_id}` == _this.$store.state.chats.current)){
					return
				}
				_this.playSound()
			})
			
			sub(`socket_addUserToGroup`,function(e){
				console.log(_this.$store.state.chats.current)
				if(e.user_id == _this.$store.state.user.user.id ||( `g_${e.group_id}` == _this.$store.state.chats.current)){
					return
				}
				_this.playSound()
			})
			
			sub(`socket_makeNewGroup`,function(e){
				console.log(_this.$store.state.chats.current)
				if(e.user_id == _this.$store.state.user.user.id ||( `g_${e.group_id}` == _this.$store.state.chats.current)){
					return
				}
				_this.playSound()
			})
			
			sub(`socket_deleteGroup`,function(e){
				uni.showToast({
					title:"群聊已删除"
				})
				_this.playSound()
			})
			
			sub(`socket_gmessage`,function(e){
				console.log(_this.$store.state.chats.current)
				if(e.user_id == _this.$store.state.user.user.id ||( `g_${e.group_id}` == _this.$store.state.chats.current)){
					return
				}
				_this.playSound()
			})
			
			sub(`socket_friendApply`,function(){
				_this.playSound()
			})
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		globalData: {

		},
		methods:{
			playSoundl:function(){
				const setting = this.$store.state.user.setting
				if(setting.mute_status){
					return
				}
				let url = '/static/sound/' + 'vdcall' + '.mp3';
				const innerAudioContext = uni.createInnerAudioContext();
				commonData.setAudioCtx(innerAudioContext)
				innerAudioContext.autoplay = true;
				innerAudioContext.src = url;
				innerAudioContext.onPlay(() => {
				  console.log('开始播放');
				});
				innerAudioContext.onError((res) => {
				  console.log(res.errMsg);
				  console.log(res.errCode);
				});
			},
			playSound:function(){
				const setting = this.$store.state.user.setting
				if(setting.mute_status){
					return
				}
				const soundMap = {
					qk:"轻快",
					lx:"流行",
					qr:"轻柔",
					dc:"短促",
				}
				let url = '/static/sound/' + 'qk' + '.mp3';
				for(let k in soundMap){
					if(soundMap[k] == setting.message_sound){
						url = '/static/sound/' + k + '.mp3'
						break
					}
				}
				const innerAudioContext = uni.createInnerAudioContext();
				innerAudioContext.autoplay = true;
				innerAudioContext.src = url;
				innerAudioContext.onPlay(() => {
				  console.log('开始播放');
				});
				innerAudioContext.onError((res) => {
				  console.log(res.errMsg);
				  console.log(res.errCode);
				});
			},
		}
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";
	/*每个页面公共css */
</style>